#!/usr/bin/python
#-*- coding=utf-8 -*-

from __future__ import print_function

import multiprocessing

import zmq
import json
import time
from constant import TaskType

class Task:
    def __init__(self, task_type, name, socket):
        """

        :task_type: 处理任务类型
        :name: 任务名字
        :socket: 服务器连接

        """
        self.__type = task_type
        self.__name = name
        self.__socket = socket

    def __str__(self):
        return self.__name

    def send_2_server(self):
        """ 将任务发给服务器
        """
        task = {'task_type': self.__type.value, 'task': self.__name}
        self.__socket.send_string(json.dumps(task))
        print(self.__name, "product", task)


#例子
def main():
    """Basic request-reply client using REQ socket."""
    socket = zmq.Context().socket(zmq.DEALER)
    socket.identity = u"Client".encode("ascii")
    socket.connect("tcp://localhost:50000")
    tasks = []
    tasks.append(Task(TaskType.A, 'Task_A_1', socket))
    tasks.append(Task(TaskType.A, 'Task_A_2', socket))
    tasks.append(Task(TaskType.B, 'Task_B_1', socket))
    tasks.append(Task(TaskType.B, 'Task_B_2', socket))

    for task in tasks:
        task.send_2_server()

    time.sleep(10)

    # Clean up
    socket.close()

if __name__ == "__main__":
    main()
